package com.cpl.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
@WebServlet(name="hello",urlPatterns = "/hello")//名字 地址
public class HelloServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("===================");
        req.setCharacterEncoding("utf-8");//响应编码utf-8
        resp.setCharacterEncoding("utf-8");//响应编码utf-8
        resp.setContentType("text/html");//设置响应头，告诉浏览器当前给你的是一个html
        String name=req.getParameter("name");//取参数的一种方法
        req.setAttribute("name",name);
//        resp.sendRedirect("/Out");//外部
        //重定向 可以访问其他站点内容
//        Writer writer = resp.getWriter();//

                req.getRequestDispatcher("/Out") .forward(req,resp); //绝对 ：/  //内部
        // 服务器内部跳转 地址不变共用一个请求对象



//        writer.write("<!DOCTYPE html>\n" +
//                "<html lang=\"en\">\n" +
//                "<head>\n" +
//                "    <title>Title</title>\n" +
//                "</head>\n" +
//                "<body>\n" +
//                "<h>welcome:"+name+"</h>\n" +
//                "<h>welcome端口:"+req.getServerPort()+"</h>\n" +端口
//                "<h>welcome:"+req.getHeader("contentype")+"</h>\n" +请求头
//                "<h>welcome完整端口号:"+req.getRequestURI()+"</h>\n" +//当前相对路径
//                "<h>welcome完整端口号:"+req.getRequestURL()+"</h>\n" +//完整路径
//                "<h>welcome参数map键值对 对象:"+req.getParameter("content name")+"</h>\n" +
//                "<h>welcome路径:"+req.getRealPath()+"</h>\n" +
//
//
//                "</body>\n" +
//                "</html>");
//        writer.flush();
//        writer.close();

    }
}
